<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .active {
            background: orange
        }
    </style>

</head>

<body>
    <button>按钮1</button>
    <button>按钮2</button>
    <button>按钮3</button>
    <button>按钮4</button>
    <script>
        // 获取每一个button 
        var btn = document.getElementsByTagName('button');
        for (var i = 0; i < btn.length; i++) {
            btn[i].onclick = function () {

                this.className = "active";

                // 页面加载完成后,for 循环已结束，但点击还未开始
                for (j = 0; j < btn.length; j++) {
                    btn[j].className = ''
                }
                this.className = "active" //给btn 添加class名
            }
        }





    </script>
</body>

</html>